
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.CellRangeAddress8Bit;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

/**
 * Created by Administrator on 2016/10/30.
 */

public class main2 {
    public void readExcelTitle(InputStream is){
        try{
            POIFSFileSystem fs=new POIFSFileSystem(is);
            HSSFWorkbook wb=new HSSFWorkbook(fs);
            HSSFSheet sheet= wb.getSheetAt(0);
            int  rowNumber=sheet.getLastRowNum();

            System.out.println("表1共有"+rowNumber+"行");
            System.out.println("请输入你要读取的单元格的行数和列数");
            Scanner s=new Scanner(System.in);
            System.out.println("行");
            int row=s.nextInt();
            System.out.println("列");
            int column=s.nextInt();
            HSSFRow myrow=sheet.getRow(row-1);
            HSSFCell cell=myrow.getCell(column-1);
            String content=cell.getStringCellValue();
            System.out.println("内容为"+content);
        }
        catch (IOException e){
            e.printStackTrace();
        }

    }
    public void setFormat(InputStream is){
        try{
            POIFSFileSystem fs=new POIFSFileSystem(is);
            HSSFWorkbook wb=new HSSFWorkbook(fs);
            HSSFSheet sheet= wb.getSheetAt(0);
            int  rowNumber=sheet.getLastRowNum();

            System.out.println("表1共有"+rowNumber+"行");
            System.out.println("请输入你要设置的单元格的行数和列数");
            Scanner s=new Scanner(System.in);
            System.out.println("行");
            int row=s.nextInt();
            System.out.println("列");
            int column=s.nextInt();
            HSSFRow myrow=sheet.getRow(row-1);
            HSSFCell cell=myrow.getCell(column-1);
            HSSFCellStyle style=wb.createCellStyle();
            style.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
            cell.setCellStyle(style);
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
    public void merge(InputStream is){
        try{
            POIFSFileSystem fs=new POIFSFileSystem(is);
            HSSFWorkbook wb=new HSSFWorkbook(fs);
            HSSFSheet sheet= wb.getSheetAt(0);
            int  rowNumber=sheet.getLastRowNum();
            System.out.println("表1共有"+rowNumber+"行");
            System.out.println("请输入你要合并的区域的第一个单元格的行号，最后一个单元格的行号，第一个单元格的列号，最后一个单元格的列号");
            Scanner s=new Scanner(System.in);
            int firstrow=s.nextInt();
            int lastrow=s.nextInt();
            int fistcolumn=s.nextInt();
            int lastcolumn=s.nextInt();
            CellRangeAddress region=new CellRangeAddress(firstrow, lastrow, firstrow, lastcolumn);
            sheet.addMergedRegion(region);
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
    public void setformula(InputStream is){
        try{
            POIFSFileSystem fs=new POIFSFileSystem(is);
            HSSFWorkbook wb=new HSSFWorkbook(fs);
            HSSFSheet sheet= wb.getSheetAt(0);
            int  rowNumber=sheet.getLastRowNum();
            System.out.println("表1共有"+rowNumber+"行");
            HSSFRow row = sheet.createRow(0);
            HSSFCell cell = row.createCell(0);
            cell.setCellFormula("2+3*4");
            cell = row.createCell(1);
            cell.setCellValue(10);
            cell = row.createCell(2);
            cell.setCellFormula("A1*B1");
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
}
